package com.example.vehicle.service.Impl;

import com.example.entity.Household;
import com.example.result.CommonResult;
import com.example.utils.MyOSSUtil;
import com.example.utils.MyPageHelper;
import com.example.vehicle.dao.VehicleMapper;
import com.example.vehicle.service.VehicleService;
import com.example.vehicle.vo.VehicleVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 停车场管理-车辆全查
     * @param vehicleCode
     * @return
     */
    @Override
    public CommonResult getVehicle(String vehicleCode) {
        return CommonResult.ok(MyPageHelper.getPageResult(vehicleMapper.getVehicle(vehicleCode)));
    }

    /**
     * 停车场管理-车辆删除
     * @param id
     * @return
     */
    @Override
    public int delVehicle(Integer id) {
//        final MyOSSUtil myOSSUtil = new MyOSSUtil();
//        myOSSUtil.uploadFile();
        int i = vehicleMapper.delVehicle(id);
        return i;
    }

    /**
     * 停车场管理-车辆增改
     * @param vehicle
     * @return
     */
    @Override
    public int saveOrUpdateVehicle(MultipartFile file, VehicleVO vehicle) throws IOException {
        if (file!=null){
            final MyOSSUtil myOSSUtil = new MyOSSUtil();
            final String s = myOSSUtil.uploadFile(file);
            vehicle.setFileId(s);
        }else{
            vehicle.setFileId("null");
        }
        int i = 0;
        if(vehicle.getId()==0){
            vehicle.setCreateTime(new Date());
            vehicle.setUpdateTime(new Date());
            vehicle.setVehicleDelFlag(0);
            i = vehicleMapper.addVehicle(vehicle);

        }else {
            vehicle.setUpdateTime(new Date());
            i = vehicleMapper.updateVehicle(vehicle);
        }
        return i;
    }

    /**
     * 停车场管理-住户全查
     * @param householdType
     * @return
     */
    @Override
    public List<Household> getHouseAll(String householdType) {
        return vehicleMapper.getHouseAll(householdType);
    }
}
